<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">货架名称</label>
        <el-input v-model="query.gsName" clearable placeholder="货架名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="货架名称" prop="gsName">
            <el-input v-model="form.gsName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="货架样式" prop="gsStyle">
            <el-select v-model="form.gsStyle" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.gs_style"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="轮播图" prop="gsImg">
            <el-select v-model="form.gsImg" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.gs_img"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="货架类型" prop="gsType">
            <el-select v-model="form.gsType" filterable placeholder="请选择" @change="Hand(form.gsType)">
              <el-option
                v-for="item in dict.gs_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="back" label="背景图片" prop="gsImage">
            <div class="el-upload">
              <img :src="form.gsImagepath ? baseApi + '/avatar/' + form.gsImagepath : Avatar" class="avatar" @click="toggleShow">
              <myUpload
                v-model="show"
                :headers="headers"
                :url="upImg"
                @crop-upload-success="cropUploadSuccess"
              />
            </div>
            <el-input v-model="form.gsImage" style="width: 370px;display: none" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="gsId" label="货架编号" />
        <el-table-column prop="gsName" label="货架名称" />
        <el-table-column prop="gsStyle" label="货架样式">
          <template slot-scope="scope">
            {{ dict.label.gs_style[scope.row.gsStyle] }}
          </template>
        </el-table-column>
        <el-table-column prop="gsImg" label="轮播图">
          <template slot-scope="scope">
            {{ dict.label.gs_img[scope.row.gsImg] }}
          </template>
        </el-table-column>
        <el-table-column prop="gsImage" label="背景图片" />
        <el-table-column prop="gsType" label="货架类型">
          <template slot-scope="scope">
            {{ dict.label.gs_type[scope.row.gsType] }}
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','gShelves:edit','gShelves:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudGShelves from '@/api/gShelves'
import { mp } from '@/api/gShelves'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import myUpload from 'vue-image-crop-upload'
import { mapGetters } from 'vuex'
import Avatar from '@/assets/images/avatar.png'
import store from '@/store'
import { getToken } from '@/utils/auth'

const defaultForm = { gsId: null, gsName: null, gsStyle: null, gsImg: null, gsImage: null, gsType: null }
export default {
  name: 'GShelves',
  components: { pagination, crudOperation, rrOperation, udOperation, myUpload },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['gs_style', 'gs_img', 'gs_type'],
  cruds() {
    return CRUD({ title: '测试题1', url: 'api/gShelves', idField: 'gsId', sort: 'gsId,desc', crudMethod: { ...crudGShelves }})
  },
  data() {
    return {
      show: false,
      Avatar: Avatar,
      back: false,
      headers: {
        'Authorization': getToken()
      },
      permission: {
        add: ['admin', 'gShelves:add'],
        edit: ['admin', 'gShelves:edit'],
        del: ['admin', 'gShelves:del']
      },
      rules: {
        gsName: [
          { required: true, message: '货架名称不能为空', trigger: 'blur' }
        ],
        gsStyle: [
          { required: true, message: '货架样式不能为空', trigger: 'blur' }
        ],
        gsImg: [
          { required: true, message: '轮播图不能为空', trigger: 'blur' }
        ],
        gsType: [
          { required: true, message: '货架类型不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'gsName', display_name: '货架名称' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'upImg',
      'baseApi'
    ])
  },
  methods: {
    toggleShow() {
      this.show = !this.show
    },
    cropUploadSuccess(jsonData, field) {
      store.dispatch('GetInfo').then((data) => {
        mp().then((data) => {
          this.form.gsImage = data.ImgPath
          const path = data.ImgPath
          const lastIndex = path.lastIndexOf('\\')
          const result = path.substr(lastIndex + 1)
          this.form.gsImagepath = result
        })
        console.log(data)
      })
    },
    Hand(a) {
      if (a === '限时抢购') {
        this.back = true
      } else {
        this.back = false
      }
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>
  .avatar {
    width: 120px;
    height: 120px;
    border-radius: 50%;
  }
</style>
